package org.hyjiacan.jcapture.deadqq.client.util;

import java.awt.AWTException;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

public class CaptureObserver implements MouseListener, MouseMotionListener {
	private Window win;
	private Point oldpos;
	private Point newpos;
	private Rectangle rect;
	private long lastClick;

	public CaptureObserver() {
		rect = new Rectangle();
		lastClick = 0;
	}

	public CaptureObserver(Window win) {
		this();
		observe(win);
	}

	public void observe(Window win) {
		this.win = win;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			if (e.getWhen() - lastClick < 500) {
				// win.setVisible(false);
				win.getWindow().dispose();
				try {
					win.hideTips();
					BufferedImage bi = new Robot().createScreenCapture(win
							.shoot());
					setClipBoard(bi);
				} catch (AWTException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

			lastClick = e.getWhen();
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		oldpos = e.getLocationOnScreen();
		rect.setLocation(oldpos.getLocation());
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		win.setTipsPosition(e.getLocationOnScreen());
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		win.setTipsPosition(e.getLocationOnScreen());
		newpos = e.getLocationOnScreen();
		if (newpos.x > oldpos.x && newpos.y > oldpos.y)
			rect.setSize(newpos.x - oldpos.x, newpos.y - oldpos.y);
		else if (newpos.x < oldpos.x && newpos.y > oldpos.y)
			rect.setSize(oldpos.x - newpos.x, newpos.y - oldpos.y);
		else if (newpos.x < oldpos.x && newpos.y < oldpos.y)
			rect.setSize(oldpos.x - newpos.x, oldpos.y - newpos.y);
		else if (newpos.x > oldpos.x && newpos.y < oldpos.y)
			rect.setSize(newpos.x - oldpos.x, oldpos.y - newpos.y);

		win.drawRect(rect);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		win.setTipsPosition(e.getLocationOnScreen());
	}

	public void draw(Rectangle rect) {
		Graphics g = win.getWindow().getGraphics();
		g.drawRect(rect.x, rect.y, rect.width, rect.height);

		win.getWindow().paintComponents(g);
		win.getWindow().repaint();
	}

	public void setClipBoard(final Image img) {
		win.getWindow().getToolkit().getSystemClipboard()
				.setContents(new Transferable() {

					@Override
					public boolean isDataFlavorSupported(DataFlavor flavor) {
						return DataFlavor.imageFlavor.equals(flavor);
					}

					@Override
					public DataFlavor[] getTransferDataFlavors() {
						return new DataFlavor[] { DataFlavor.imageFlavor };
					}

					@Override
					public Object getTransferData(DataFlavor flavor)
							throws UnsupportedFlavorException {
						if (isDataFlavorSupported(flavor)) {
							return img;
						} else {
							throw new UnsupportedFlavorException(flavor);
						}
					}
				}, null);
	}
}
